<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends Controller
{
	private $_template_f;
	private $_lang;
	function __construct()
	{
		parent::Controller();
		$this->_template_f = $this->config->item('template_f');
		$this->_lang = $this->mycommon->getLangCode();
		if($this->session->userdata('uname'))
		{
			redirect($this->config->item('base_url'));
			die();
		}
		$this->load->model('login/login_model');
		$this->load->language('login/login', $this->_lang);
		
		$this->load->library('myencrypt');
	}
	
	function _loadHeader($data)
	{
		$header = array();
		$header['title'] = ($data['title'] == '') ? 'Self serving - Login' : $data['title'];
		$header['template_f'] = $this->_template_f;
		$header['numCartItem'] = 0;
		$header['username'] = '';
		$header['userid'] = '';
		$header['module_name'] = 'login';
		$header['lang'] = $this->_lang;
		
		// load header
		$this->load->view($this->_template_f . 'preheader_view');
		$this->load->view($this->_template_f . 'header_view', $header);
	}
	
	function _loadMenu()
	{
		$data = array();
		$data['username'] = '';
		$data['userid'] = '';
		$data['permitid'] = '';
		$data['lang'] = $this->_lang;
		$data['common_lang'] = $this->lang->line('common_lang');
		$this->load->view($this->_template_f . 'menu_view', $data);
	}
	
	function _loadFooter()
	{
		$this->load->view($this->_template_f . 'footer_view');
	}
	
	
	function index()
	{
		$data = array();
		$data['login_lang'] = $this->lang->line('login_lang');
		$data['error'] = '';
		$data['form_title'] = $data['login_lang']['login_title'];
		$currUrl =$this->input->get('url');
		
		$currUrl = $currUrl != '' ? $currUrl : $this->config->item('base_url');
		$data['currUrl'] = $currUrl;
		if($this->input->post('txtUser'))
		{
			$username = $this->input->post('txtUser', true);
			$password = $this->input->post('txtPass', true);
			$password = md5($password);
			
			$row = $this->login_model->check_login($username, $password);
			if(!empty($row))
			{
				if($row['username'] != '' && $row['user_id'] != '' && $row['permitID'] == -1)
				{
					//$isp = ($row['permitID'] == -1 || $row['permitID'] == 0) ? Myencrypt::Encode($row['user_id']) : '';
					//$buname = ($row['permitID'] == -1 || $row['permitID'] == 0) ? $row['username'] : '';
					
					$isp = ($row['permitID'] == -1) ? Myencrypt::Encode($row['user_id']) : '';
					$pisp = ($row['plusPermitID'] == -1) ? Myencrypt::Encode($row['user_id']) : '';
					$misp = ($row['mobilePermitID'] == -1) ? Myencrypt::Encode($row['user_id']) : '';
					$buname = ($row['permitID'] == -1 || $row['plusPermitID'] == -1 || $row['mobilePermitID'] == -1) ? $row['username'] : '';
					
					$this->session->set_userdata('uname', $row['username']);
					$this->session->set_userdata('uid', $row['user_id']);
					$this->session->set_userdata('permitid', $row['permitID']);
					$this->session->set_userdata('pluspermitid', $row['plusPermitID']);
					$this->session->set_userdata('mpermitid', $row['mobilePermitID']);
					$this->session->set_userdata('lang', $row['lang']);
					$this->session->set_userdata('groupid', $row['groupid']);
					$this->session->set_userdata('allowfree', $row['allowfree']);
					$this->session->set_userdata('isp', $isp);
					$this->session->set_userdata('pisp', $pisp);
					$this->session->set_userdata('misp', $misp);
					$this->session->set_userdata('issale', $row['issale']);
					$this->session->set_userdata('buname', $buname);
					$this->session->set_userdata('sysdate', $row['_sysdate']);
					redirect(urldecode($currUrl));
					die();
				}
				else
				{
					$data['error'] = 'Invalid username or password or access deny!';
				}
			}
			else
			{
				$data['error'] = 'Invalid username or password or access deny!';
			}
			
		}
		
		$header = array();
		$header['title'] = $data['form_title'];
		
		//load header
		$this->_loadHeader($header);
		
		// load menu
		$this->_loadMenu();
		
		$this->load->view($this->_template_f . 'login/login_view', $data);
		// load footer
		$this->_loadFooter();
		
	}
}